/*分页控件=====================*/
/*初始化*/
/*绑定回车到查询按钮*/
$(function(){
	$(document).keydown(function(){ 
		if(event.keyCode==13){ 
		$("#le-search").trigger('click');
		return false;
		}
	});
});
var xingmingsj;
var kaishisj;
var jieshusj;
var quanxiancgmz = true;
var dwpanduan=true;
var pageNumber=1;
function getLiebiaocs(quanxian,prehuanzheid,prepageNo,prekaishisj,prejieshusj,prexingmingsj){
	if(prehuanzheid!='null'&&prehuanzheid!=null){
		dwhuanzheid=prehuanzheid;
		pageNumber=prepageNo;
		kaishisj=prekaishisj;
		jieshusj=prejieshusj;
		if(prexingmingsj!=''&&prexingmingsj!='null'){
			xingmingsj=prexingmingsj;
			$(".xingmingsj").val(xingmingsj);
		}
	}else{
		pageNumber=1;
		xingmingsj='';
		jieshusj='';
		kaishisj='';
	}
	quanxiancgmz = quanxian;
	showHzlb();
}
// 定位参数
var dwhuanzheid;
var dwpageNo;
var dwkaishisj;
var dwjieshusj;
var dingwei;
var dwxingmingsj;
//清空查询条件
$('#huanzhejl-chaxchongzhi').click(function(){
	$('.xingmingsj').val('');
	$('#hzlb-kaishjsj').val('');
	$('#hzlb-jieshusj').val('');
	xingmingsj = $('.xingmingsj').val();
	kaishisj = '';
	jieshusj = '';
	pageNumber=1;
	dwpanduan=false;
	showHzlb();
	return false; 
});

//查询按钮
$(document).on('click',".hz-search",function(){
	xingmingsj = $.trim($('.xingmingsj').val());
	kaishisj = $('#hzlb-kaishjsj').val();
	jieshusj = $('#hzlb-jieshusj').val();
	pageNumber=1;
	dwpanduan=false;
    showHzlb();
    return false; 
});
//新建按钮
$("#huanzhejl-xinjian").click(function(){	
		$("#cedan").load(path+"addHuanzhe.html");
		return false;
	});

//数据读取
var xingmingsj = $(".xingmingsj").val();
function showHzlb(){
	var totalNumber = 0;
    var params={
        "xingmingsj":xingmingsj,
        "kaishisj":kaishisj,
        "jieshusj":jieshusj,
    };
    
    $.ajax({
        //请求方式为get
        type:"get",
        //获取json文件路径
        url:"./huanzhe/getCount",
        data:params,
    	async:false,
        //请求成功完成后要执行的方法
        success: function(data){
	    	totalNumber = data;
	    	if(totalNumber==0){
				$("#hz-listdiv").css("display","none");
				$("#hz-img").html("<img src='"+path+"/image/chaxtj/cxtj-nobody.png'>");
			}else{
				$("#hz-listdiv").css("display","block");
				$("#hz-img").html("");
			}
    	}
    });
    
    function createHzlb(name) {
    	dwxingmingsj=encodeURI(encodeURI($(".xingmingsj").val()));
    	dwjieshusj=$('#hzlb-jieshusj').val();
		dwkaishisj=$('#hzlb-kaishjsj').val();
    	$('#tbhuanzhe').html('');
		var container = $('#pagination-'+name);
		container.pagination({
		dataSource:'./huanzhe/getByPage?kaishisj='+encodeURI(kaishisj)+'&jieshusj='+encodeURI(jieshusj)+'&xingmingsj='+encodeURI(xingmingsj),
		locator: 'entities',
		pageSize: 12,
		pageNumber:pageNumber,
		totalNumber: totalNumber,
		showNavigator: true,
   	    formatNavigator: '共 <%= totalNumber %> 条',
		callback: function(data, pagination){
    			var content = '';
            $.each(data,function(index,value){
            	var sex = "";
            	if(data[index].xingbie==null || data[index].xingbie =="undefined" || data[index].xingbie =="" ){
                    sex="";
                }
            	if(data[index].xingbie==1){
               	 	sex="男";
                }else if(data[index].xingbie==2){
               	 	sex="女";
                }
            	if(data[index].nianling==null) {
            		var nianling = "";
            	}else{
            		var nianling=data[index].nianling+data[index].nianlingdw;
            	}
            	if(data[index].shoujihm==null) {
            		var shoujihm = "";
            	}else{
            		var shoujihm=data[index].shoujihm;
            	}
            	
            	var jibingmc = '';
            	if(data[index].jibingmc!=null) {
            		jibingmc = data[index].jibingmc;
            		if(jibingmc.length>15) {
            			jibingmc = jibingmc.substring(0,15)+'...';
            		}
            	}
            	var jiuzhenrq = '';
            	if(data[index].jiuzhenrq!=null) {
            		jiuzhenrq = data[index].jiuzhenrq;
            	} 
            	var jiuzhencs = this.jiuzhencs;
            	if(jiuzhencs==0) {
            		jiuzhencs = '';
            	}
            	var qianfeijlid = this.qianfeijlid;
            	var huankuanjlid = this.huankuanjlid;
            	var canbaoryid = this.canbaoryid;
            	var pageNo=pagination.pageNumber;
            	var perPageRecordNum=pagination.pageSize;
            	content=content+"<tr id='huanzhe"+this.id+"'>";
            	
            	content=content+'<td style="width:5%;"><label>'+(((pageNo-1)*perPageRecordNum)+index+1)+'</label></td>';
                if(isYiBao && isKeHud && qianfeijlid!=null&&huankuanjlid==null&&canbaoryid!=null) {
                	content=content+'<td class="left" style="width:12%;color:red; display: none;" ><span class="yibaohz a-btnyibao" title="医保患者"></span><span class="kun qian" title="该患者含有欠款未还" onclick="toHuankuan('+this.id+','+pageNo+')">欠</span>' +data[index].xingming + "</td>" ;
                }else if(isYiBao && isKeHud && canbaoryid!=null){
                	content=content+'<td class="left" style="width:12%; display: none;" ><span class="yibaohz a-btnyibao" title="医保患者"></span>' +data[index].xingming + "</td>" ;
                }else if(qianfeijlid!=null&&huankuanjlid==null&&QIANFEIQX) {
                	content=content+'<td class="left" style="width:12%;color:red;" ><span class="kun qian" title="该患者含有欠款未还" onclick="toHuankuan('+this.id+','+pageNo+')">欠</span>' +data[index].xingming + "</td>" ;
                }else{
                	content=content+'<td class="left" style="width:12%;" ><span class="kun "></span>' +data[index].xingming + "</td>" ;
                }
                content=content+'<td style="width:7%;">' +sex + "</td>" ;
                content=content+'<td style="width:6%;">' +nianling+' </td>' ;
                content=content+'<td style="width:12%;">' +shoujihm +"</td>" ;
                content=content+'<td style="width:12%;">'+jiuzhenrq+'</td>' ;
                content=content+'<td style="width:8%;">'+jiuzhencs+'</td>' ;
                content=content+'<td class="left" style="width:20%;">'+jibingmc+'</td>' ;
                content=content+'<td style="width:18%;" class="left"><a class="hz-operation-left"  id="'+data[index].id+'" onclick="toHuanzhexq('+data[index].id+','+pageNo+')">详情</a>';
                if(quanxiancgmz) {
                	content=content+'<a class="hz-operation-middle"  id="'+data[index].id+'" onclick="toZhenliao('+data[index].id+')">诊疗</a>';
                }
                if (data[index].yewurk == 1 || data[index].yewurk == 3) {
	                content=content+'<a class="hz-operation-right"  id="'+data[index].id+'" onclick="toJiuzjl('+data[index].id+','+pageNo+')">就诊记录</a></td>';
                }
                content=content+"</tr>";
                if(dwhuanzheid==this.id){
					dingwei=this.id;
	        	}
            });
             $("#tbhuanzhe").html(content);
		    if (isKeHud == true && isYiBao == true) {
				$('.a-btnyibao').css('display','inline-block');
				$('.a-nryibao').css('display','block');
		    } else {
				$('.a-btnyibao').css('display','none');
				$('.a-nryibao').css('display','none');
		    }
             if(dingwei!=null&&dingwei!=''){
	 	        $('#tbhuanzhe').find('tr[id="huanzhe'+dingwei+'"]').addClass('list_tr');
	        }
	        if(!dwpanduan){
	        	$('#tbhuanzhe').find('tr[id="huanzhe'+dingwei+'"]').removeClass('list_tr');
	        }
        }

    });
		return container;
    };
    createHzlb('fenyhz');
		if(totalNumber <= 12) {
			$('#pagination-fenyhz').css('display','none');
		}else{
			$('#pagination-fenyhz').css('display','block');
		}
}

var row_id = 0;
	
//弹出框
function confrim(){
	 if($(".operation-right[index='"+row_id+"']").attr("value")== "禁用" ){
         var jinyongbz=0;
     }
     else if($(".operation-right[index='"+row_id+"']").attr("value")== "启用"  ){
    	 var jinyongbz=1;
     }
	 var sendData1 ={
		 "id":row_id,
		 "jinyongbz":jinyongbz
	 };
	  $.ajax({
	        //请求方式为get
	        type:"POST",
	        //获取json文件路径
	        url:"./yuangong/jinyongyg",
	        //传送数据
	        data:sendData1,
	        //请求成功完成后要执行的方法
	        success: function(data){
	        	reset();
	        	 $("#prohibit").css("display","none");
	        	 $("#awaken").css("display","none");
	        }
	    });
	  return false;
}
$('#hzlb-kaishjsj').val('')
//时间插件
function myDateTimePicker(firstDate,secondDate){

	var dbkaishisj;
	var dbjieshusj;
	$.ajax({
		type:"post",
		url:"./base/getShujukuTime",
		async:false,
		success: function(data){
		dbkaishisj=data.dbMonthFirstDay;
		dbjieshusj=data.dbCurrentDate;
		}
	});
	getShujukuTime(dbjieshusj);
    $(firstDate).datetimepicker({
   	    format:"L",
        allowInputToggle:true,
    	showTodayButton:true,
    	});
    $(secondDate).datetimepicker({
   	 	format:"L",
        allowInputToggle:true,
    	showTodayButton:true,
    	});
    
    $(firstDate).on("dp.change", function (e) {
        $(secondDate).data("DateTimePicker").minDate(e.date);
    });
    $(secondDate).on("dp.change", function (e) {
        $(firstDate).data("DateTimePicker").maxDate(e.date);
    });
}

//弹出框确认
$(".confrim").click(function(){
	confrim();
});

//弹出框取消
$(".canceal").click(function(){
	 $("#prohibit").css("display","none");
	 $("#awaken").css("display","none");
});
//进入还款模块详情
function toHuankuan(preid,prepageNo){
	window.location.href=path+"/views/huankuan/huankuan-detail.jsp?ID="+preid+"&rukou=huanzhelb"+"&pageNo="+prepageNo+"&kaishisj="+dwkaishisj+"&jieshusj="+dwjieshusj+"&xingmingsj="+dwxingmingsj;
}
//进入患者详情页面
function toHuanzhexq(id,prepageNo){
	$("#cedan").load(path+"/views/huanzhe/huanzhe-detail.jsp?ID="+id+"&pageNo="+prepageNo+"&kaishisj="+dwkaishisj+"&jieshusj="+dwjieshusj+"&xingmingsj="+dwxingmingsj);
}
//进入诊疗页面
function toZhenliao(id){
	var huanzZhenliao= encodeURI(encodeURI(true));
	window.location.href= path+"/views/changguimz/changguimz.jsp?huanzZhenliao="+huanzZhenliao+"&ID="+id;
}
//进入就诊记录
function toJiuzjl(id,prepageNo){
	$("#cedan").load(path+"/views/huanzhe/huanzhe-record.jsp?ID="+id+"&pageNo="+prepageNo+"&kaishisj="+dwkaishisj+"&jieshusj="+dwjieshusj+"&xingmingsj="+dwxingmingsj);
}
